import { ElLoading } from "element-plus";
import $ from "jquery";

let showType;
export const loading = {
  loadingInstance: null,
  show(options) {
    showType = "type1";
    this.close();
    this.loadingInstance = ElLoading.service({
      ...options,
      target: options.target || "body",
    });
  },
  setText(newText) {
    this.loadingInstance?.setText(newText);
  },
  close() {
    this.loadingInstance?.close();
    this.loadingInstance = null;
    $("#appLoading").hide();
    if (showType === "type2") {
      $(".zhongzhao").removeClass("no-active");
      document.body.dataset.noActive = "";
      $("#boxLoadingType2").hide();
    }
  },
  showType2() {
    showType = "type2";
    $(".zhongzhao").addClass("no-active");
    document.body.dataset.noActive = 1; // 禁止页面点击
    // document.body.style.cursor = "progress";
    $("#boxLoadingType2").show();
  },
};

document.body.addEventListener(
  "click",
  event => {
    //通过 .no-active，应该可以控制住交互，防止没处理全，这里再拦截下点击事件
    if (document.body.dataset.noActive) {
      event.stopPropagation();
    }
  },
  true
);

window.loading = loading;
